<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-01-01 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		$page = $this->page_num + 1;
		
		$mid = isset($this->request['mid']) ? (int)$this->request['mid'] : '';
		$keyword = isset($this->request['keyword']) ? trim($this->request['keyword']) : '';
		
		//$query_link = "admin.php?m=movie_refer&page={$page}&count={$this->page_limit}";
		$query_link = "admin.php?m=movie_refer";
		$where_list = array();
		
		if($mid)
		{
			$where_list['mid'] = "mr.movie_id = '$mid'";
			$query_link .= "&mid=".urlencode($mid);
		}
		if($keyword)
		{
			$where_list['refer'] = "lower(mr.`refer`) like lower('%$keyword%')";
			$query_link .= "&keyword=".urlencode($keyword);
		}
		
		$where = empty($where_list) ? '' : 'where '.implode(' and ', $where_list).' ';
		
		$sql = " select count(1) as `total_record` from `".TABLE_PREFIX."movie_refer` mr {$where}";
		$total_record = DBFunction::getInstance()->getOne($sql);

		$item_list = array();
		if($total_record > 0)
		{
			$offset = $this->page_num * $this->page_limit;
			$sql = "select mr.*, m.name from `".TABLE_PREFIX."movie_refer` mr 
					inner join `".TABLE_PREFIX."movie` m on mr.movie_id = m.id
					{$where}
					order by mr.`id` desc limit {$offset},{$this->page_limit} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
		}
				
		$page_html = page($total_record, $this->page_limit, $page, $query_link, '20 50 100 200 500');	
		include template('admin/movie_refer');
	}
	
	private function add()
	{
		$request = array();
		if(isset($this->request['c']))
		{
			$request = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$request['movie_id'] = isset($request['movie_id']) ? $request['movie_id'] : '';
		$request['refer'] = isset($request['refer']) ? $request['refer'] : '';
		
		$action = 'do_add';
		$action_name = '添加影片经典语录';
		include template('admin/movie_refer_info');
	}
	
	private function doAdd()
	{
		$request['movie_id'] = isset($this->request['movie_id']) ? (int)$this->request['movie_id'] : '';
		$request['refer'] = isset($this->request['refer']) ? trim($this->request['refer']) : '';
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "admin.php?m=movie_refer&a=add&c=$key";
		
		$movie = get_movie_info($request['movie_id']);
			
		if(!isset($movie['id']) || !$movie['id'])
		{
			$this->messager("影片不存在", $redirect);
		}
		if(strlen($request['refer']) > 999)
		{
			$this->messager("内容超长", $redirect);
		}
		
		$result = add_movie_refer($request);
		
		if($result)
		{
			$this->messager("添加成功", 'admin.php?m=movie_refer');
		}
		else
		{
			$this->messager("添加失败", $redirect);
		}
	}
	
	private function modify()
	{
		$refer_id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$refer_id)
		{
			$this->messager("请指定ID", 'admin.php?m=movie_refer');
		}
		
		$request = array();
		if(isset($this->request['c']))
		{
			$request = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		if(!check_array($request))
		{
			$request = get_movie_refer($refer_id);
		}
		
		$request['movie_id'] = isset($request['movie_id']) ? $request['movie_id'] : '';
		$request['refer'] = isset($request['refer']) ? $request['refer'] : '';
		
		$action = 'do_modify&id='.$refer_id;
		$action_name = '编辑影片经典语录';
		include template('admin/movie_refer_info');
	}
	
	private function doModify()
	{
		$refer_id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$refer_id)
		{
			$this->messager("请指定ID", 'admin.php?m=movie_refer');
		}
		
		$request['id'] = $refer_id;
		$request['movie_id'] = isset($this->request['movie_id']) ? (int)$this->request['movie_id'] : '';
		$request['refer'] = isset($this->request['refer']) ? trim($this->request['refer']) : '';
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "admin.php?m=movie_refer&a=modify&c=$key";
		
		$refer = get_movie_refer($refer_id);
			
		if(!isset($refer['id']) || !$refer['id'])
		{
			$this->messager("内容不存在", $redirect);
		}
		if(strlen($request['refer']) > 999)
		{
			$this->messager("内容超长", $redirect);
		}
		
		$result = update_movie_refer($request);
		
		if($result)
		{
			$this->messager("修改成功", 'admin.php?m=movie_refer');
		}
		else
		{
			$this->messager("修改失败", $redirect);
		}
	}
	
	private function doDelete()
	{
		$refer_ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		
		if(check_array($refer_ids))
		{
			$refer_ids = implode(',', $refer_ids);
		}
		
		if(!$refer_ids)
		{
			$this->messager("请指定ID", 'admin.php?m=movie_refer');
		}
		
		$request['ids'] = $refer_ids;
		
		$result = delete_movie_refer($request);
		
		if($result)
		{
			$this->messager("删除成功", 'admin.php?m=movie_refer');
		}
		else
		{
			$this->messager("删除失败", 'admin.php?m=movie_refer');
		}
	}
}